package evaluation.pattern;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

public class PatternEvaluater {
	private Vector<PatternElement> patternVector = new Vector<PatternElement>();
	public PatternEvaluater(String PatternFilename,String FPFilename, String TPFilename)
	{
		this.ReadPatternFile(PatternFilename);
		this.elvaluate(FPFilename, TPFilename);
	}
	private void ReadPatternFile(String filename)
	{
		try {
			BufferedReader freader = new BufferedReader(new FileReader(filename));
			String line;
			while((line=freader.readLine())!=null)
			{
				if(line.length()>0)
				{
					PatternElement pattern = new PatternElement(line);
					this.patternVector.add(pattern);
				}
			}
			freader.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void sortPatternByLength()
	{
		int size = this.patternVector.size();
		for(int i=0;i<size;i++)
		{
			int min = Integer.MAX_VALUE;
			int minIndex = -1;
			for(int j=i;j<size;j++)
			{
				PatternElement currentElement = this.patternVector.elementAt(j);
				if(currentElement.getLength()<min)
				{
					min = currentElement.getLength();
					minIndex = j;
				}
			}
			PatternElement minElement = this.patternVector.elementAt(minIndex);
			
			this.patternVector.set(minIndex, this.patternVector.elementAt(i));
			this.patternVector.set(i, minElement);
		}
	}
	private void elvaluate(String FPFilename, String TPFilename)
	{
		try {
			BufferedReader freader = new BufferedReader(new FileReader(FPFilename));
			String line;
			while((line=freader.readLine())!=null)
			{
				int[] nums = this.getPatternNumsFromLine(line);
				for(int num : nums)
				{
					PatternElement pattern = this.patternVector.elementAt(num-1);
					pattern.addWrongNum();
				}
			}
			freader.close();
			freader = new BufferedReader(new FileReader(TPFilename));
			while((line=freader.readLine())!=null)
			{
				int[] nums = this.getPatternNumsFromLine(line);
				for(int num : nums)
				{
					PatternElement pattern = this.patternVector.elementAt(num-1);
					pattern.addCorrectNum();
				}
			}
			freader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private int[] getPatternNumsFromLine(String line)
	{
		Vector<Integer> numVector = new Vector<Integer>();
		int pos = line.indexOf('|');
		pos += "|Reason=\"".length();
		int posEnd = line.indexOf('"', pos);
		String reason = line.substring(pos, posEnd);
		reason = reason.toLowerCase();
		pos = line.indexOf("Pattens:");
		pos += "Pattens:".length();
		posEnd = line.indexOf('"', pos);
		String patternStr = line.substring(pos, posEnd);
		String[] parts = patternStr.split("\\s+");
		for(String part : parts)
		{
			pos = part.indexOf(':');
			if(pos<0) continue;
			String numStr = part.substring(0, pos);
			String reasonToken = part.substring(pos+1).toLowerCase();
			if(reason.contains(reasonToken))
			{
				int num = Integer.parseInt(numStr);
				if(!numVector.contains(num)) numVector.add(num);
			}
		}
		int size = numVector.size();
		int[] nums = new int[size];
		for(int i=0;i<size;i++)
		{
			nums[i]=numVector.elementAt(i);
		}
		return nums;
	}
	public void PrintSholdDeletePatters()
	{
		for(int i=0;i<this.patternVector.size();i++)
		{
			PatternElement pattern = this.patternVector.elementAt(i);
			if(pattern.sholdDelete())
			{
				System.out.println("#"+(i+1)+" "+pattern.toString());
			}
		}
	}
	public void WriterLeftPatternsToFile(String filename)
	{
		try {
			BufferedWriter fwriter = new BufferedWriter(new FileWriter(filename));
			for(int i=0;i<this.patternVector.size();i++)
			{
				PatternElement pattern = this.patternVector.elementAt(i);
				if(!pattern.sholdDelete())
				{
					fwriter.write(pattern.getPattern());
					fwriter.newLine();
				}
			}
			fwriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void WrtierAllPatternsToFile(String filename)
	{
		try {
			BufferedWriter fwriter = new BufferedWriter(new FileWriter(filename));
			for(int i=0;i<this.patternVector.size();i++)
			{
				PatternElement pattern = this.patternVector.elementAt(i);
				if(!pattern.sholdDelete())
				{
				fwriter.write(pattern.toString());
				fwriter.newLine();
				}
			}
			fwriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
